package com.cms.attachment.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cms.common.annotation.Log;
import com.cms.common.core.controller.BaseController;
import com.cms.common.core.domain.AjaxResult;
import com.cms.common.enums.BusinessType;
import com.cms.attachment.domain.CmsAttachment;
import com.cms.attachment.service.ICmsAttachmentService;
import com.cms.common.utils.poi.ExcelUtil;
import com.cms.common.core.page.TableDataInfo;

/**
 * 文章附件Controller
 * 
 * @author rjxy
 * @date 2025-09-29
 */
@RestController
@RequestMapping("/attachment/attachment")
public class CmsAttachmentController extends BaseController
{
    @Autowired
    private ICmsAttachmentService cmsAttachmentService;

    /**
     * 查询文章附件列表
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsAttachment cmsAttachment)
    {
        startPage();
        List<CmsAttachment> list = cmsAttachmentService.selectCmsAttachmentList(cmsAttachment);
        return getDataTable(list);
    }

    /**
     * 导出文章附件列表
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:export')")
    @Log(title = "文章附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsAttachment cmsAttachment)
    {
        List<CmsAttachment> list = cmsAttachmentService.selectCmsAttachmentList(cmsAttachment);
        ExcelUtil<CmsAttachment> util = new ExcelUtil<CmsAttachment>(CmsAttachment.class);
        util.exportExcel(response, list, "文章附件数据");
    }

    /**
     * 获取文章附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cmsAttachmentService.selectCmsAttachmentById(id));
    }

    /**
     * 新增文章附件
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:add')")
    @Log(title = "文章附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsAttachment cmsAttachment)
    {
        return toAjax(cmsAttachmentService.insertCmsAttachment(cmsAttachment));
    }

    /**
     * 修改文章附件
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:edit')")
    @Log(title = "文章附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsAttachment cmsAttachment)
    {
        return toAjax(cmsAttachmentService.updateCmsAttachment(cmsAttachment));
    }

    /**
     * 删除文章附件
     */
    @PreAuthorize("@ss.hasPermi('attachment:attachment:remove')")
    @Log(title = "文章附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmsAttachmentService.deleteCmsAttachmentByIds(ids));
    }
}
